/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fasea.ui.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lars-wolfram
 */
public abstract class InputDelayer {

    private int delayInMilliseconds = 0;
    private int delayCounter = 0;
    private boolean isRunning = false;
    private static final int DELAY_STEP = 10;
    private final ExecutorService service = Executors.newFixedThreadPool(1);
    private final Runnable myThread = new Runnable() {
            public void run() {
                isRunning = true;
                while (InputDelayer.this.delayCounter < InputDelayer.this.delayInMilliseconds) {
                    try {
                        Thread.sleep(DELAY_STEP);
                        InputDelayer.this.delayCounter += DELAY_STEP;
                    } catch (InterruptedException ex) {
                        Logger.getLogger(InputDelayer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                InputDelayer.this.delayCounter = 0;
                isRunning = false;
                doSomething();
            }
        };

    public InputDelayer(int delayInMilliseconds) {
        this.delayInMilliseconds = delayInMilliseconds;
        
    }

    public void run() {
        if(!isRunning()){
            service.execute(myThread);
        }else{
            reset();
        }
    }

    private void reset() {
        delayCounter = 0;
    }

    private boolean isRunning() {
        return isRunning;

    }

    public abstract void doSomething();
}
